package com.zima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zima.mall.entity.GoodsCategory;
import com.zima.mall.entity.MallGoods;
import com.zima.mall.mapper.CategoriesMapper;
import com.zima.mall.mapper.ProductsMapper;
import com.zima.mall.service.ProductService;
import com.zima.mall.vo.TableVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductsMapper productsMapper;

    @Resource
    private CategoriesMapper categoriesMapper;

    @Override
    public TableVo<MallGoods> findAllGoods(Integer page,Integer size) {
        QueryWrapper wrapper = new QueryWrapper();
        IPage<MallGoods> mallGoodsIPage = new Page<>(page,size);
        IPage<MallGoods> result = productsMapper.selectPage(mallGoodsIPage,wrapper);
        TableVo tableVo = new TableVo();

        tableVo.setData(result.getRecords());
        tableVo.setTotalNum(productsMapper.selectCount(wrapper));

        return tableVo;
    }

    @Override
    public int deleteProduct(Integer id) {
        if (id == null) return 0;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        int num = 0;
        num = productsMapper.delete(wrapper);
        return num;
    }

    @Override
    public int addProduct(MallGoods product) {
        int num = 0;
        num = productsMapper.insert(product);
        return num;
    }

    @Override
    public TableVo findAllCategories() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("id","name");
        TableVo<GoodsCategory> tableVo = new TableVo<>();

        tableVo.setData(categoriesMapper.selectList(wrapper));
        tableVo.setTotalNum(categoriesMapper.selectCount(null));

        return tableVo;
    }

    @Override
    public TableVo findAllHot() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_hot",1);

        TableVo<MallGoods> mallGoodsTableVo = new TableVo<>();
        mallGoodsTableVo.setData(productsMapper.selectList(wrapper));
        mallGoodsTableVo.setTotalNum(productsMapper.selectCount(wrapper));
        return mallGoodsTableVo;
    }

    @Override
    public Integer editProduct(MallGoods product) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",product.getId());
        int num = 0;
        num = productsMapper.update(product,updateWrapper);
        return num;
    }

}
